﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Stala;
using System.Windows;

namespace TestApp
{
	class CheckVersionCommand :ICommand
	{
		private IVersionFinder<Version> versionFinder;

		public IVersionFinder<Version> VersionFinder { 
			get { return versionFinder; }
			set
			{
				// Remove old event binding
				if (versionFinder != null)
					versionFinder.LatestVersionFound -= latestVersionFound;

				versionFinder = value;
			
				// Bind events
				versionFinder.LatestVersionFound += latestVersionFound;
				CanExecuteChanged(this, null);
			}
		}

		public bool CanExecute(object parameter)
		{
			if(versionFinder != null)
				return true;
			return false;
		}

		public event EventHandler CanExecuteChanged;
		public event Action<Version> NewVersionFound;

		public void Execute(object parameter)
		{
			versionFinder.FindLatestVersion();
		}

		void latestVersionFound(Version version)
		{
			NewVersionFound(version);
		}
	}
}
